package jia.core.graphics;

import jia.game.GenericFlags;
import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.Map;

// ====================================================

/**
 * @author Alec Lanter
 */
public class WallImageManager {
    private static Map<String, WallImage> parsedWallImages = new HashMap<String, WallImage>();
    private static Map<String, WallImageParser> parsers = new HashMap<String, WallImageParser>();
    
    private static final Logger log = Logger.getLogger(WallImageManager.class);
    
    // ====================================================
    /**
     */
    private WallImageManager() {
        // GNDN
    }
    
    // ====================================================
    /**
     * @param resourceName
     * @return
     */
    public static WallImage getWallImage(String resourceName,
        String parserClassName) {
        String wallImageKey = parserClassName + ":" + resourceName;
        if (!parsedWallImages.containsKey(wallImageKey)) {
            if (!parsers.containsKey(parserClassName)) {
                initParser(parserClassName);
            }
            
            WallImageParser parser = parsers.get(parserClassName);
            
            if (parser != null) {
                parsedWallImages.put(wallImageKey, parser.getWallsetImage(resourceName));
            }
        }
        
        return parsedWallImages.get(wallImageKey);
    }
    
    // ====================================================
    /**
     * @param parserClassName The parserClassName to set.
     */
    public static void initParser(String parserClassName) {
        try {
            WallImageParser parser = (WallImageParser) Class.forName(parserClassName)
            .newInstance();
            parsers.put(parserClassName, parser);
        } catch (Exception e) {
            if (GenericFlags.errorMessages){   
                log.fatal("Could not create WallImageParser of type '" + parserClassName
                    + "'");
            }
        }
    }
    
    //===============================================
    /**
     */
    public static void purge() {
        parsedWallImages.clear();
        parsers.clear();
    }
}
